<template>
  <div class="goods-image">
    <!-- 放大镜显示区域 -->
    <div
      class="large"
      v-show="!isOutside"
      :style="[
        {
          backgroundImage: `url(${Img[curIndex]})`,
          backgroundPositionX: `${bgPos.x}px`,
          backgroundPositionY: `${bgPos.y}px`,
        },
      ]"
    ></div>
    <!-- middle盒子是滑块滑动区 -->
    <div class="middle" ref="target">
      <!-- 当前显示的主图 -->
      <img :src="Img[curIndex]" />
      <!-- 蒙层容器 -->
      <div
        class="layer"
        v-show="!isOutside"
        :style="{ left: left + 'px', top: top + 'px' }"
      ></div>
    </div>
    <ul class="small">
      <!-- 主图列表  :class="{ active: curIndex === i }"动态添加类名-->
      <li
        v-for="(img, index) in Img"
        :key="index"
        @mouseenter="curIndex = index"
      >
        <img :src="img" alt="" />
      </li>
    </ul>
  </div>
</template>

<script>
import { ref, watch } from 'vue'
import { useMouseInElement } from '@vueuse/core'
export default {
  name: 'GoodsImage',
  props: {
    // 商品主图列表数据
    Img: {
      type: Array,
      default: () => [],
    },
  },
  setup() {
    // 需求:鼠标移动到小图上 左边显示对应的大图   思路:记录当前鼠标移动到的小图索引号  根据索引显示大图
    const curIndex = ref(0)
    // 滑块位置数据
    const left = ref(0) // 滑块距离左侧的距离
    const top = ref(0) // 滑块距离顶部的距离
    // 获取Dom
    const target = ref(null)
    // 计算大图显示位置
    const bgPos = ref({ x: 0, y: 0 })
    // elementX 鼠标基于target左上角X轴偏移  elementY 鼠标基于target左上角Y轴偏移  isOutside 鼠标是否在target外
    // 三个参数都是ref对象
    const { elementX, elementY, isOutside } = useMouseInElement(target)
    watch([elementX, elementY], () => {
      // 监听处理
      // 横向X
      if (elementX.value < 100) {
        // 左侧边界
        left.value = 0
      } else if (elementX.value > 300) {
        // 右侧边界
        left.value = 200
      } else {
        left.value = elementX.value - 100
      }
      // 纵向Y
      if (elementY.value < 100) {
        // 上侧边界
        top.value = 0
      } else if (elementY.value > 300) {
        // 下侧边界
        top.value = 200
      } else {
        top.value = elementY.value - 100
      }
      // 移动放大大图
      bgPos.value.x = -left.value * 2
      bgPos.value.y = -top.value * 2
    })
    return { curIndex, target, top, left, bgPos, elementX, elementY, isOutside }
  },
}
</script>

<style lang="less" scoped>
.large {
  position: absolute;
  top: 0;
  left: 412px;
  width: 400px;
  height: 400px;
  z-index: 500;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  background-repeat: no-repeat;
  background-size: 800px 800px;
  background-color: #f8f8f8;
}
.layer {
  width: 200px;
  height: 200px;
  background: rgba(0, 0, 0, 0.2);
  left: 0;
  top: 0;
  position: absolute;
}
.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;
  .middle {
    width: 400px;
    height: 400px;
    background-color: #f5f5f5;
  }
  .small {
    width: 80px;
    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;
      &:hover,
      &.active {
        border: 2px solid @xtxColor;
      }
    }
  }
}
</style>
